﻿using System;
using System.Xml;
using biz.ritter.javapi.util;
using biz.ritter.javapi.xml;

namespace Nofs.Net.nofs.metadata.XMLDriver
{
    public sealed class XMLDomainObject
    {
        private String _className;
        private String _providesNameMethod;
        private String _needsContainerMethod;
        private String _needsContainerManagerMethod;
        private bool _canWrite;
        private XMLFolderDescription _folderDescription;
        private LinkedList<String> _executableMethods;
        private LinkedList<String> _folderMethods;
        private String _providesCreateTimeMethod;
        private String _providesLastAccessTimeMethod;
        private String _providesLastModifiedTimeMethod;
        private String _providesUIDMethod;
        private String _providesGIDMethod;
        private String _providesModeMethod;

        public void ReadFromXML(XmlNode domainNode) //throws Exception 
        {
            _providesCreateTimeMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesCreateTime", null);
            _providesLastAccessTimeMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesLastAccessTime", null);
            _providesLastModifiedTimeMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesLastModifiedTime", null);
            _providesUIDMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesUID", null);
            _providesGIDMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesGID", null);
            _providesModeMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesMode", null);
            _className = XMLHelper.TryReadChildContents(domainNode, "ClassName", null);
            _providesNameMethod = XMLHelper.TryReadChildContents(domainNode, "ProvidesNameMethod", null);
            _needsContainerMethod = XMLHelper.TryReadChildContents(domainNode, "NeedsContainerMethod", null);
            _needsContainerManagerMethod = XMLHelper.TryReadChildContents(domainNode, "NeedsContainerManagerMethod", null);
            _canWrite = Boolean.Parse(XMLHelper.TryReadChildContents(domainNode, "CanWrite", "false"));
            Branch folderNode = XMLHelper.TryToFindChildByName(domainNode, "FolderDescription");
            if (folderNode != null)
            {
                _folderDescription = new XMLFolderDescription();
                _folderDescription.ReadFromXML(folderNode);
            }
            LinkedList<String> executableMethods = new LinkedList<String>();
            LinkedList<String> folderMethods = new LinkedList<String>();
            foreach (Branch executableMethod in XMLHelper.FindChildrenByName(domainNode, "ExecutableMethod"))
            {
                executableMethods.add(executableMethod.getText());
            }
            foreach (Branch folderMethod in XMLHelper.FindChildrenByName(domainNode, "FolderMethod"))
            {
                folderMethods.add(folderMethod.getText());
            }
            if (_executableMethods == null)
            {
                _executableMethods = new LinkedList<string>();
            }
            _executableMethods.addAll(Collections.unmodifiableList(executableMethods));

            if (_folderMethods == null)
            {
                _folderMethods = new LinkedList<string>();
            }
            _folderMethods.addAll(Collections.unmodifiableList(folderMethods));
        }

        public String ClassName() { return _className; }
        public String ProvidesNameMethod() { return _providesNameMethod; }
        public String NeedsContainerMethod() { return _needsContainerMethod; }
        public String NeedsContainerManagerMethod() { return _needsContainerManagerMethod; }
        public bool CanWrite() { return _canWrite; }
        public XMLFolderDescription FolderDescription() { return _folderDescription; }
        public LinkedList<String> ExecutableMethods() { return _executableMethods; }
        public LinkedList<String> FolderMethods() { return _folderMethods; }
        public String ProvidesCreateTimeMethod() { return _providesCreateTimeMethod; }
        public String ProvidesLastAccessTimeMethod() { return _providesLastAccessTimeMethod; }
        public String ProvidesLastModifiedTimeMethod() { return _providesLastModifiedTimeMethod; }
        public String ProvidesUIDMethod() { return _providesUIDMethod; }
        public String ProvidesGIDMethod() { return _providesGIDMethod; }
        public String ProvidesModeMethod() { return _providesModeMethod; }
    }
}
